#include "Http.hpp"
std::string RequestStr(const HttpRequest &req)
{
    std::stringstream ss;
    ss<<req._method<<" "<<req._path<<" "<<req._version<<"\r\n";
    for(auto& e:req._params)
    {
        ss<<e.first<<": "<<e.second<<"\r\n";
    }
    for(auto& e:req._headers)
    {
        ss<<e.first<<": "<<e.second<<"\r\n";
    }
    ss<<"\r\n";
    ss<<req._body;
    return ss.str();
}
void hello(const HttpRequest &req,HttpResponse *resp)
{
    resp->SetContent(RequestStr(req),"text/plain");
    //sleep(15);
}
void login(const HttpRequest &req,HttpResponse *resp)
{
    resp->SetContent(RequestStr(req),"text/plain");
}
void putfile(const HttpRequest &req,HttpResponse *resp)
{
    std::string body = req._body;
    std::string pathname = "./wwwroot/"+req._path;
    Util::WriteFile(pathname,body);
}
void delfile(const HttpRequest &req,HttpResponse *resp)
{
    resp->SetContent(RequestStr(req),"text/plain");
}
int main()
{
    HttpServer server(8600);
    server.SetBaseDir("./wwwroot/");
    server.SetThreadCount(3);
    server.Get("/hello",hello);
    server.Post("/login",login);
    server.Put("/1234.txt",putfile);
    server.Delete("/1234.txt",delfile);
    server.Start();
    return 0;
}